﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.Reflection;

namespace TTApp.Phone.Silverlight.Hexagons
{
    public sealed class ImageConverter : TypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                Image image = new Image();

                Uri uri = new Uri(String.Format("TTApp.Phone.Silverlight;component/{0}", value), UriKind.Relative);
                StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);

                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(resourceInfo.Stream);

                image.Source = bitmapImage;

                //image.Source = new ImageSourceConverter().ConvertFromString(value as string) as ImageSource;

                return image;
            }

            return null;
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;

            return false;
        }
    }
}
